package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.ChineseCharacterUtil;
import ysq.xyg.host.dao.HostCityDao;
import ysq.xyg.host.model.dto.CityDTO;
import ysq.xyg.host.model.dto.CityDataDTO;
import ysq.xyg.host.model.dto.HostCityDto;
import ysq.xyg.host.model.po.HostCity;
import ysq.xyg.host.model.vo.HostCityVo;
import ysq.xyg.host.service.HostCityService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房产-城市表service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:23:39
 */
@Service("hostCityService")
public class HostCityServiceImpl extends BaseServiceImpl<HostCity> implements HostCityService {

    @Autowired
    private HostCityDao hostCityDao;

    @Autowired
    public void setBaseDao(HostCityDao hostCityDao) {
        this.baseDao = hostCityDao;
    }

    /**
     * 分页查询
     *
     * @param hostCityVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:23:39
     */
    @Override
    public ReturnEntity page(HostCityVo hostCityVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" order_num asc, update_time desc");
        List<HostCity> list = acceptList(hostCityVo);
        PageInfo<HostCity> page = new PageInfo<HostCity>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostCityVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:23:39
     */
    @Override
    @CacheEvict(value={"cacheHostCityDto"}, allEntries=true)
    public ReturnEntity addOrUpdate(HostCityVo hostCityVo, SysUser currentUser) {
        boolean isUpate = true;
        HostCity hostCity;
        if (StrUtil.isEmpty(hostCityVo.getId())) {
            hostCity = new HostCity(currentUser);
            isUpate = false;
        } else {
            hostCity = selectByPrimaryKey(hostCityVo.getId());
            if (ObjectUtil.isNull(hostCity)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostCity.setUpdateTime(new Date());
            hostCity.setUpdateUser(currentUser.getId());
        }
        hostCity.setName(hostCityVo.getName());
        hostCity.setDescription(hostCityVo.getDescription());
        hostCity.setOrderNum(hostCityVo.getOrderNum());
        int num;
        if (isUpate) {
            num = updateByPrimaryKey(hostCity);
        } else {
            num = insert(hostCity);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:23:39
     */
    @Override
    @CacheEvict(value = {"cacheHostCityDto"}, allEntries = true)
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostCity hostCity = selectByPrimaryKey(id);
        if (hostCity != null) {
            hostCity.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostCity.setUpdateTime(new Date());
            hostCity.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostCity);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostCityVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:23:39
     */
    @Override
    public List<HostCity> acceptList(HostCityVo hostCityVo){
        Example example = new Example(HostCity.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostCityVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostCityVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostCityVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostCityVo.getAccountId());
        }
        if (StringUtil.isNotEmpty(hostCityVo.getName())) {
            criteria.andLike("name", "%" + hostCityVo.getName() + "%");
        }
        example.setOrderByClause(" order_num asc, update_time desc");
        return hostCityDao.selectByExample(example);
    }

    @Override
    public List<HostCityDto> selectHostCityDtoList(String accountId) {
        return hostCityDao.selectHostCityDtoList(accountId);
    }

    @Override
    public ReturnEntity acceptSelect(String code, String accountId) {
        HostCityVo hostCityVo = new HostCityVo();
        hostCityVo.setAccountId(accountId);
        List<HostCity> list = acceptList(hostCityVo);
        List<SysDictContent> sysDictContentList = new ArrayList<>();
        for (HostCity entity : list) {
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }

    @Override
    public List<CityDTO> selectHostCityJson(String accountId, String name) {
        HostCityVo hostCityVo = new HostCityVo();
        hostCityVo.setAccountId(accountId);
        hostCityVo.setName(name);
        List<HostCity> cities = this.acceptList(hostCityVo);
        return buildHostCity(cities);
    }

    private List<CityDTO> buildHostCity(List<HostCity> hostCityList) {
        List<CityDTO> cityList = new ArrayList<CityDTO>();
        if (hostCityList == null || hostCityList.size() == 0) {
            return cityList;
        }
        for (char a = 'A'; a <= 'Z'; a++) {
            // 查询数据库，或者比较
            List<CityDataDTO> cityDataList = new ArrayList<CityDataDTO>();
            // 需要key和data
            for (HostCity hostCity : hostCityList) {
                // 取出首字符
                String letter = ChineseCharacterUtil.convertHanzi2Pinyin(hostCity.getName(), false).toUpperCase();
                if (StringUtils.equals(letter.subSequence(0, 1), String.valueOf(a))) {
                    cityDataList.add(new CityDataDTO(hostCity.getId(), hostCity.getName()));
                }
            }
            if (cityDataList == null || cityDataList.size() == 0) {
                continue;
            }
            cityList.add(new CityDTO(String.valueOf(a), cityDataList));
        }
        return cityList;
    }
}
